<?php
/**
 * Link Management Administration Screen.
 */
/** Load WordPress Administration Bootstrap */
require_once ('admin.php');
global $current_user;
$featured = DealFlight::FEATURED_NO;
$wp_list_table = _get_list_table('WP_Links_List_Table');
$doaction = $wp_list_table->current_action();
$aImgages = array();

if ($doaction && isset($_REQUEST['linkcheck'])) {
    check_admin_referer('bulk-bookmarks');
    if ('delete' == $doaction) {
        $bulklinks = (array) $_REQUEST['linkcheck'];
        foreach ($bulklinks as $link_id) {
            $link_id = (int) $link_id;
            wp_delete_link($link_id);
        }
        wp_redirect(add_query_arg('deleted', count($bulklinks), admin_url('link-manager.php')));
        exit;
    }
} elseif (!empty($_GET['_wp_http_referer'])) {
    wp_redirect(remove_query_arg(array('_wp_http_referer', '_wpnonce'), wp_unslash($_SERVER['REQUEST_URI'])));
    exit;
}
$sucessful = 0;
$error = array();
$fileName = '';
$ok = 1;
/*
 * Submit
 */
if (isset($_REQUEST['publish_product'])) {

    if ($_REQUEST['name'] == "") {
        $error['name'] = "Name can not be blank";
        $ok = 0;
    }
    
    if(isset($_FILES['image'])){
	
	/**
	 * validate image
	 */
	for($i = 0; $i < count($_FILES['image']['name']); $i++){
	    if ($_FILES['image']['name'][$i]) {	
		 $name_img_small = $_FILES['image']['name'][$i];
		$ds_name_img_small = explode('.', $name_img_small);
		$n_ds_name_img_small = (int) count($ds_name_img_small) - 1;
		$duopifile = $ds_name_img_small[$n_ds_name_img_small];
		$fileName = time() . '.' . $duopifile;
		
		if (($duopifile == 'jpg') || ($duopifile == "JPG") || ($duopifile == "png") ||
			($duopifile == "PNG") || ($duopifile == "gif") || ($duopifile == "GIF") ||
			($duopifile == "jpeg") || ($duopifile == "JPEG")) {
		  
		} else {
		    $error['image'] = "Please upload a valid image";
		    $ok = 0;break;
		}
	    }
	}
	/**
	 * 
	 */
	if($ok == 1){
	    for($i = 0; $i < count($_FILES['image']['name']); $i++){
		if ($_FILES['image']['name'][$i]) {

		    $name_img_small = $_FILES['image']['name'][$i];
		    $ds_name_img_small = explode('.', $name_img_small);
		    $n_ds_name_img_small = (int) count($ds_name_img_small) - 1;
		    $duopifile = $ds_name_img_small[$n_ds_name_img_small];
		    $fileName = uniqid() . '.' . $duopifile;
		    
		    move_uploaded_file($_FILES['image']['tmp_name'][$i], DEAL_PATH . $fileName);
		    array_push($aImgages, $fileName);
		   
		}
	    }
	}
    }
    if ($_REQUEST['description'] == "") {
        $error['description'] = "Description can not be blank";
        $ok = 0;
    }

    if ($_REQUEST['price'] == "") {
        $error['price'] = "Price can not be blank";
        $ok = 0;
    } else {
        if (preg_match('/^[^0-9\.]/', $_REQUEST['price'])) {
            $error['price'] = "Price must be a number";
            $ok = 0;
        }
    }

    if ($_REQUEST['discount'] == "") {
        $error['discount'] = "Discount can not be blank";
        $ok = 0;
    } else {
        if (preg_match('/^[^0-9\.]/', $_REQUEST['discount'])) {
            $error['discount'] = "Discount must be a number";
            $ok = 0;
        }
    }

    if ($_REQUEST['categogy'] == "") {
        $error['categogy'] = "You must choose category";
        $ok = 0;
    }
    if ($_REQUEST['start_time'] == "") {
        $error['start_time'] = "Start time can not be blank";
    }
    if ($_REQUEST['end_time'] == "") {

        $error['end_time'] = "End time can not be blank";
    }



    if ($_REQUEST['start-location'] == "") {
        $error['start_location'] = "Leaving from can not be blank";
        $ok = 0;
    }
    if ($_REQUEST['end_location'] == "") {
        $error['end_location'] = 'Going to can not be blank';
        $ok = 0;
    }



    if ($ok == 1) {
        $sucessful = 1;
        DealFlight::add(DealFlight::DEAL, $current_user->ID, $_REQUEST['name'], $_REQUEST['description'], $aImgages, 
		$_REQUEST['price'], $_REQUEST['discount'], $_REQUEST['start_time'], $_REQUEST['end_time'], DealFlight::FEATURED_NO,
		$_REQUEST['order'], $_REQUEST['categogy'],0, $_REQUEST['start-location'], $_REQUEST['end_location'], 
		$_REQUEST['adult'], $_REQUEST['children'], $_REQUEST['infant'], $_REQUEST['class'], $_REQUEST['city']);
        wp_redirect(admin_url() . 'deals-list.php');
    }
}
?>    
<?php include_once ('./admin-header.php'); ?>
<link rel="stylesheet" type="text/css" href="css/tuan.css"/>

<div class="wrap nosubsub">
    <h2>Add New Deal<?php
if (!empty($_REQUEST['s']))
    printf('<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', esc_html(wp_unslash($_REQUEST['s'])));
?>
    </h2>

    <form id="posts-filter" method="post"  enctype="multipart/form-data"> 
        <?php $wp_list_table->search_box(__('Search Links'), 'link'); ?>
        <hr>
        <div style="width:100%; background:#FDFDFD">
            <table width="707"  height="77" cellspacing="0" >
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp"><?php _e('Name') ?> (*)</p>  
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px;">:  			    
                            <input  style="width:80%; height:30px;" name="name" type="text" id="name"   
                                    value="<?Php
        if (isset($_POST['name'])) {
            echo $_POST['name'];
        }
        ?>"  class="regular-text"> 
                            <span class="errorclass"><Br/><?php echo $error['name']; ?></span>
                        </div>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Description'); ?> (*)</p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <?php echo wp_editor($_POST['description'], 'description', array('textarea_name' => 'description')); ?>
                        <span class="errorclass"><br/><?php echo $error['description']; ?></span>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Image') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <input type="file" name="image[]"/>
			<br/>
			<a href="javascript:void(0)" class="add-file button button-primary button-large"> + Add file </a>
                        <span class="errorclass"><br/><?php echo $error['image']; ?></span>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left"><label for="siteupdates">
                            <p class="labelp" ><?php _e('Price') ?>
                                (*): </p> </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <input type='text' name='price' value='<?php echo $_POST['price'] ?>'/>
                            <span class="errorclass"><br/><?php echo $error['price']; ?></span>
                        </div>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left"><label for="siteupdates">
                            <p class="labelp" ><?php _e('Discount') ?>
                            </p> </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <input type='text' name='discount' value='<?php echo $_POST['discount'] ?>'/>
                            <span class="errorclass"><br/><?php echo $error['discount']; ?></span>
                        </div>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Order in list of featured deals') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <select name='order'>
                                <option value='1'>1</option>
                                <option value='2'>2</option>
                                <option value='3'>3</option>
                                <option value='4'>4</option>
                            </select>
                        </div>
                    </td>
                </tr> 
                 <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Leaving from') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top"  class="td-type-flight">
                        <input value="<?php
                        if (isset($_REQUEST['start-location'])) {

                            echo $_REQUEST['start-location'];
                        }
                        ?>" placeholder="Enter City/State" type="text"
                               id="leaving-from" name="start-location">
                        <span class="errorclass" >
                            <?php echo $error['start_location']; ?>
                        </span>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Going to') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top" class="td-type-flight" >
                        <input value="<?php
                        if (isset($_REQUEST['end_location'])) {

                            echo $_REQUEST['end_location'];
                        }
                        ?>" placeholder="Enter City/State" type="text" 
                               id="end_location" name="end_location">
                        <span class="errorclass"><?php echo $error['end_location']; ?></span>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates"> Departure date</label>
                    </th>
                    <td width="563"  valign="top" >
                        <input  value="<?php
                        if (isset($_REQUEST['start_time'])) {
                            echo $_REQUEST['start_time'];
                        }
                        ?>" id="start_time"    type="text" name="start_time" />
                        <img id="image_bt_start_time" width="20px;" src="<?php echo get_template_directory_uri(); ?>/images/icon_datetime.png"/>
                        <p class="errorclass"><?php echo $error['start_time']; ?></p>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates"> Return date</label>
                    </th>
                    <td width="563"  valign="top" >
                        <input  value="<?php
                                if (isset($_REQUEST['end_time'])) {

                                    echo $_REQUEST['end_time'];
                                }
                        ?>" id="end_time"  type="text" name="end_time" />
                        <img id="image_bt_end_time" width="20px;" src="<?php echo get_template_directory_uri(); ?>/images/icon_datetime.png"/>
                        <p class="errorclass"><?php echo $error['end_time']; ?></p>
                    </td>
                </tr>
                 <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Adult(12+) </label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php
                        $adult = -1;                        
                        if(isset($_REQUEST['adult'])) {  $adult = $_REQUEST['adult']; }                        
                        ?>
                        <select  id="adult" name="adult" class="select-find-flights" >
                            <?php
                            for ($i = 1; $i <= 9; $i++) {
                                if ($adult == $i) {
                                    echo '<option selected=selected value="' . $i . '" >' . $i . '</option>';
                                } else {
                                    echo '<option value="' . $i . '" >' . $i . '</option>';
                                }
                            }
                            
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Children(2-12)</label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php
                         $children = 0;
                         if(isset($_REQUEST['children']))
                         {
                            $children = $_REQUEST['children'];
                            
                         }
                        
                        ?>
                        <select  id="children" name="children" class="select-find-flights" >
                            <?php
                            for ($i = 0; $i < 9; $i++) {

                                if ($i == $children) {

                                    echo '<option selected=selected  value="' . $i . '" >' . $i . '</option>';
                                } else {
                                    echo '<option value="' . $i . '" >' . $i . '</option>';
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Infant(0-2)</label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php
                        $infant =0; 
                        if(isset($_REQUEST['infant'])){
                            $infant = $_REQUEST['infant'];
                        }
                        ?>
                        <select id="infant" name="infant"  class="select-find-flights">

                            <option <?php
                            if ($infant == 0) {
                                echo 'selected=selected';
                            }
                            ?> >0</option>
                            <option <?php
                            if ($infant == 1) {
                                echo 'selected=selected';
                            }
                            ?> >1</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Class</label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php $class_f = $flightInfo[0]->class; ?>
                        <select name="class" id="class" class="select-find-flights">
                            <option value="<?php echo DealFlight::ECONOMY; ?>"  
                            <?php
                            if ($class_f == DealFlight::ECONOMY) {
                                echo 'selected=selected';
                            }
                            ?>>Economy</option>
                            <option value="<?php echo DealFlight::BUSINESS; ?>" 
                            <?php
                            if ($class_f == DealFlight::BUSINESS) {
                                echo 'selected=selected';
                            }
                            ?> >Business</option>
                            <option value="<?PHP echo DealFlight::FIRST_CLASS ?>" 
                            <?php
                            if ($class_f == DealFlight::FIRST_CLASS) {
                                echo 'selected=selected';
                            }
                            ?> >First Class</option>
                            <option value="<?php echo DealFlight::PREMIUM_ECONOMY ?>
                            <?php
                            if ($class_f == DealFlight::PREMIUM_ECONOMY) {
                                echo 'selected=selected';
                            }
                            ?> ">Premium Economy</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">City</label>
                    </th>
                    <td width="563"  valign="top" >

                        <select name="city" id="city" class="select-find-flights" >
                            <?php
                            $city = $flightInfo[0]->city;
                            ?>

                            <option  <?php
                            if ($city == 'USA') {
                                echo 'selected="selected"';
                            }
                            ?> value="USA">USA</option>
                          
                            <option  <?php
                            if ($city == 'India') {
                                echo 'selected="selected"';
                            }
                            ?> value="India">India</option>
                            
                              <option value="Dubai"  <?php
                            if ($city == 'Dubai') {
                                echo 'selected="selected"';
                            }
                            ?> > Dubai </option>
                        </select>
                    </td>
                </tr>
                
                <?php
                /*
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Categogy ') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <?php
                            $listCategoriesDeal = Category::getAllCategoryByType(Category::DEAL);
                            // print_r($listCategoriesDeal);

                            if ($listCategoriesDeal <> "") {
                                ?>
                                <select name='categogy' id="categogy">
                                    <option value="">Choose Category</option>
                                    <?php
                                    foreach ($listCategoriesDeal as $row) {
                                        $idCat = $row->category_id;
                                        $nameCat = $row->name;
                                        ?>

                                        <option value="<?php echo $idCat ?>">
                                            <?php echo $nameCat ?>
                                        </option>
                                        <?php
                                    }
                                }
                                ?>

                            </select>
                            <span class="errorclass"><br/><?php echo $error['categogy']; ?></span>
                        </div>
                    </td>
                </tr> 
                */
                ?>
                 <input type="hidden" id="categogy" name="categogy" value="0" />
                <tr>
                    <th valign="top"  scope="row" align="left">&nbsp;</th>
                    <td  valign="top"><div style="margin-top:10px; margin-left:5px;">
                            <input type="submit" name="publish_product" id="publish_product" 
                                   class="button button-primary button-large" value="ADD" accesskey="p"></div>
                    </td>
                </tr>
            </table>
        </div>
    </form>

</div>


<link rel="stylesheet" type="text/css" href="../wp-content/themes/travel_donald/css/jquery.datetimepicker.css" />
<script src="../wp-content/themes/travel_donald/js/jquery.js"></script>
<script src="../wp-content/themes/travel_donald/js/jquery.datetimepicker.js"></script>

<script type="text/javascript" lang="javascript">

    $('#image_bt_start_time').click(function() {
        $('#start_time').datetimepicker('show'); //support hide,show and destroy command
    });



    $('#image_bt_end_time').click(function() {
        $('#end_time').datetimepicker('show'); //support hide,show and destroy command
    });

    $(function() {
        $('#start_time').datetimepicker({
            format: 'd/m/Y',
            onShow: function(ct) {
                this.setOptions({
                    maxDate: $('#end_time').val() ? $('#end_time').val() : false
                })
            },
            timepicker: false
        });
        $('#end_time').datetimepicker({
            format: 'd/m/Y',
            onShow: function(ct) {
                this.setOptions({
                    minDate: $('#start_time').val() ? $('#start_time').val() : false
                })
            },
            timepicker: false
        });
    });




</script>
<?php
include('./admin-footer.php');
?>
<script src="<?php bloginfo('template_directory')?>/js/admin/file.js"></script>

